package com.guigu.guliware.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.gulicommon.entity.pojo.res.Res;
import com.guigu.gulicommon.entity.vo.FareVo;
import com.guigu.gulicommon.entity.vo.MemberAddressVo;
import com.guigu.gulicommon.exception.BaseException;
import com.guigu.guliware.entity.WmsWareInfo;
import com.guigu.guliware.feign.MemberServer;
import com.guigu.guliware.mapper.WmsWareInfoMapper;
import com.guigu.guliware.service.IWmsWareInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Random;

import static com.guigu.gulicommon.constant.enumes.res.SelectResCode.SELECT_FARE_SUCCESS;
import static com.guigu.gulicommon.constant.enumes.res.SelectResCode.SELECT_MEMBER_RECEIVE_ADDRESS_LIST_SUCCESS;

/**
 * <p>
 * 仓库信息 服务实现类
 * </p>
 *
 * @author 陈俊杰
 * @since 2024-08-24
 */
@Service
public class WmsWareInfoServiceImpl extends ServiceImpl<WmsWareInfoMapper, WmsWareInfo> implements IWmsWareInfoService {
    @Autowired
    private MemberServer memberServer;

    /**
     * 获取运费信息
     *
     * @param addrId 收货地址id
     * @return 邮费信息
     */
    @Override
    public Res<FareVo> getFare(Long addrId) {
        Res<MemberAddressVo> memberAddressVoRes = memberServer.getById(addrId);
        if (!memberAddressVoRes.getCode().equals(SELECT_MEMBER_RECEIVE_ADDRESS_LIST_SUCCESS.getCode())) {
            throw new BaseException(memberAddressVoRes.getCode(), memberAddressVoRes.getMsg());
        }
        //TODO 模拟运费
        Random random = new Random();

        // 生成 0 到 9 之间的一个随机整数
        int randomNumber = random.nextInt(10);
        BigDecimal decimal = new BigDecimal(randomNumber);
        FareVo fareVo = new FareVo(memberAddressVoRes.getData().get(0), decimal);
        return new Res<>(SELECT_FARE_SUCCESS, fareVo);
    }
}
